// pages/row/row.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    userInfo: '',
    tableType:[],
    row:{},
    order:{},
  },
  showPopup() {
    this.setData({ show: true });
  },
  toMenu(event){
    wx.switchTab({
			url: "/pages/menu/menu",
		})
  },
  selectType(e){
    let page = this;
    let type = e.target.dataset.type;
    let user = page.data['userInfo'];
    wx.request({
      url: 'http://test.com:3000/table/tableInfo',
      method:'post',
      data:{type:type.tabletype_people},
      success(res){
        if(res.statusCode=='200'){
          if(res.data.count>0){
            let param = {
              user_id:user.user_id,
              table_id:res.data.data[0].table_id,
            };
            wx.request({
              url: 'http://test.com:3000/row/addOrderTable',
              method:'POST',
              data: param,
              success(res){
                if(res.statusCode=='200'){
                  wx.setStorageSync('order', res.data.data);
                  page.setData({
                    order:res.data.data,
                    show: false
                  })
                  wx.switchTab({
                    url: "/pages/menu/menu",
                  })
                }
              }
            })
          }else{
            let param = {
              type:type.tabletype_number,
              tabletype_id:type.tabletype_id,
              user_id:user.user_id,
            };
            wx.request({
              url: 'http://test.com:3000/row/addRow',
              method:'post',
              data:param,
              success(res){
                if(res.statusCode=='200'){
                  let data = res.data.data;
                  data.tabletype_people = type.tabletype_people;
                  wx.setStorageSync('row', data);
                  page.setData({
                    row:data,
                    show: false
                  })
                }
              }
            })
          }
        }
      }
    })
  },
  onShua(){
    let page =this;
    let user = wx.getStorageSync('user')[0];
    wx.request({
      url: 'http://test.com:3000/row/getRow',
      method:'POST',
      data:{user_id:user.user_id},
      success(res){
        if(res.statusCode=='200'){
          if(res.data.code == '201'){
            wx.setStorageSync('row', null);
            wx.setStorageSync('order', res.data.data);
            page.setData({
              row:null,
              order:res.data.data
            })
          }else{
            let data = res.data.data;
            wx.setStorageSync('row', data);
            page.setData({
              row:data,
            })
          }
          
        }
      }
    })
  },
  onClose() {
    this.setData({ show: false });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let page = this;
    wx.request({
      url: 'http://test.com:3000/tabletype/tabletypeInfo',
      method:'GET',
      success(res){
        if(res.statusCode=='200'){
          page.setData({
            tableType : res.data.data,
          })
        }
      }
    })
    let user = wx.getStorageSync('user')[0];
    let row = wx.getStorageSync('row')==''?null:wx.getStorageSync('row');
    let order = wx.getStorageSync('order')==''?null:wx.getStorageSync('order');
    this.setData({
      userInfo: user,
      row:row,
      order:order,
    })
    this.getTabBar().setData({
			active: this.data.tabid
		})
  },

  onJump() {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})